# local data work - mapping #
# census boundry
# https://www2.census.gov/geo/tiger/TIGER2018PLtest/PLACE/2010/
# https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html



rm(list = ls())
setwd("F:/fiscal research/mayor")


library(sf)
library(maps)
library(usmap)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(classInt)

library(tidycensus)
library(tmap)
library(tmaptools)
library(leaflet)
library(tigris)
options(tigris_class = "sf")


# Data
data <- read.csv("city_list_map.csv", comment.char="" , 
                 colClasses=c(geo_fips='character'), strip.white=FALSE)

data <- subset(data, !is.na(geo_fips))

data$GEOID <- data$geo_fips

colnames(data)


data$st_cty <- paste(data$state, data$county, sep = "")

data<-data[!(data$statefip==2),]






# Map from Census shapefile
map <- st_read("cb_2021_us_place_500k/cb_2021_us_place_500k.shp")
map


map<-map[!(map$STATEFP=="02" | map$STATEFP=="15" | map$STATEFP=="60" |
             map$STATEFP=="66" | map$STATEFP=="69" | map$STATEFP=="72" |
             map$STATEFP=="78"),]

st_write(map, "map_place.shp", delete_layer = TRUE)

map_place <- st_read("map_place.shp")
map_place


ggplot(map_place) +
  geom_sf()

colnames(map_place)


geom_point(data = data, aes(x = longitude, y = latitude), size = 4, 
           shape = 23, fill = "darkred")


ggplot(map_place) + 
  geom_sf(aes(fill=data$geo_fips), color = NA) +
  geom_sf(fill = "transparent", color = "gray20", size = 0.3, 
          data = . %>% group_by(map_place$GEOID) %>% summarise()) +
  scale_fill_continuous(name="EBB Subscription Rate (%)", 
                        low = "white", high = "green4", na.value = "grey50") 
+
  theme_void() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.title = element_text(size=8),
        legend.text = element_text(size=8),
        legend.position = c(0.85, 0.20)) 


ggsave("ebb_subs_cty_ratio.pdf", width=10, height=8)
ggsave("ebb_subs_cty_ratio.png", width=10, height=8)














